import { DataTypes, Model, Optional, Sequelize } from 'sequelize';
import { sequelize } from '../config/database';

interface CircleCommentLikeAttributes {
    id: number;
    circleCommentId: number;
    userId: number;
    createdAt?: Date;
    updatedAt?: Date;
}

interface CircleCommentLikeCreationAttributes extends Optional<CircleCommentLikeAttributes, 'id' | 'createdAt' | 'updatedAt'> { }

export class CircleCommentLike extends Model<CircleCommentLikeAttributes, CircleCommentLikeCreationAttributes> implements CircleCommentLikeAttributes {
    public id!: number;
    public circleCommentId!: number;
    public userId!: number;

    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    public static associate(models: {}) {
        // 定义关联，尽管多对多关联已在 CircleComment 和 User 模型中定义
        // 这里可以添加其他关联，例如 belongsTo CircleComment 和 belongsTo User
    }
}

export function initCircleCommentLike(sequelize: Sequelize) {
    CircleCommentLike.init({
        id: {
            type: DataTypes.INTEGER.UNSIGNED,
            autoIncrement: true,
            primaryKey: true,
        },
        circleCommentId: {
            type: DataTypes.INTEGER.UNSIGNED,
            allowNull: false,
            references: {
                model: 'circle_comments',
                key: 'id',
            },
            onUpdate: 'CASCADE',
            onDelete: 'CASCADE',
        },
        userId: {
            type: DataTypes.INTEGER.UNSIGNED,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id',
            },
            onUpdate: 'CASCADE',
            onDelete: 'CASCADE',
        },
    }, {
        tableName: 'circle_comment_likes',
        sequelize,
        timestamps: true,
        indexes: [
            { // 创建复合唯一索引，确保一个用户只能点赞一个评论一次
                unique: true,
                fields: ['circleCommentId', 'userId']
            }
        ]
    });
}

export default CircleCommentLike; 